package com.jcompressor.preferences;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.jdom.Document;
import org.jdom.Element;

import com.jcompressor.entities.Project;
import com.jcompressor.utils.JcompressorUtils;

public class ConfigurationHandler {
	
	private PropertyExpander expander;
	
	public static List<String> parseJcompressorConfiguration(final String config) {
		final List<String> configurations = new ArrayList<String>();
		final Document document = JcompressorUtils.loadConfiguration(config);
		final Element projectsNode = document.getRootElement().getChild("projects");
		if (projectsNode != null) {
			for (final Object obj : projectsNode.getChildren("project")) {
				final String configuration = ((Element)obj).getText();
				final File[] files = JcompressorUtils.getFileList(configuration);
				if (files.length > 0) {
					for (final File file : files) {
						configurations.add(file.getPath());
					}
				}
			}
		}
		
		return configurations;
	}
	
	public Project parseProjectConfiguration(final File config, final Map<String, String> extraProperties) {
		final Document document = JcompressorUtils.loadConfiguration(config);
		final Element projectNode = document.getRootElement().getChild("project");
		final Element propertiesNode = projectNode.getChild("properties");
		
		final Project project = new Project();
		project.setPath(StringUtils.substringBeforeLast(config.getPath(), ConfigKeys.FILE_SEPARATOR));
		
		if (projectNode != null) {
			for (final Object obj : propertiesNode.getChildren()) {
				final Element property = (Element)obj;
				if (StringUtils.isNotBlank(property.getName()) && StringUtils.isNotBlank(property.getText())) {
					project.addProperty(property.getName(), property.getText());
				}
			}
			
			final Map<String, String> props = project.getProperties();
			if ((extraProperties != null) && !extraProperties.isEmpty()) {
				props.putAll(extraProperties);
			}
			
			if (props.size() > 0) {
				this.expander = new PropertyExpander(props, "${", "}");
			}
		}
			
		project.setTitle(projectNode.getChildText("title"));
		
		final Element configurationNode = projectNode.getChild("configuration");
		project.setScriptOutput(this.expander.expandProperties(configurationNode.getChildText("scriptOutput")));
		project.setStyleOutput(this.expander.expandProperties(configurationNode.getChildText("styleOutput")));
		
		final Element optionsNode = configurationNode.getChild("yuiOptions");
		if (optionsNode != null) {
			for (final Object obj : optionsNode.getChildren()) {
				final Element option = (Element)obj;
				final StringBuffer arg = new StringBuffer("--");
				arg.append(option.getName());
				project.addOption(arg.toString());
				
				if (StringUtils.isNotBlank(option.getText())) {
					project.addOption(option.getText());
				}
			}
		}
		
		final Element filesNode = projectNode.getChild("files");
		final Element scriptsNode = filesNode.getChild("scripts");
		if (scriptsNode != null) {
			for (final Object obj : scriptsNode.getChildren("script")) {
				final Element script = (Element)obj;
				project.addScript(this.expander.expandProperties(script.getText()));
			}
		}
		
		final Element stylesNode = filesNode.getChild("styles");
		if (stylesNode != null) {
			for (final Object obj : stylesNode.getChildren("style")) {
				final Element style = (Element)obj;
				project.addStyle(this.expander.expandProperties(style.getText()));
			}
		}
		
		return project;
	}
	
}